﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace APP.BaseClass
{
    public static class StringCompartExtensions
    {

        public static bool Match(this StringCompareType compareType, string strToBeChecked, string baseStr, bool ignoreCase = false)
        {
            bool matched;


            if (string.IsNullOrEmpty(strToBeChecked))
            {
                strToBeChecked = string.Empty;  // 简化后续的比较
            }

            switch (compareType)
            {
                //case StringCompareType.StartsWith:
                //case StringCompareType.EndsWith:
                //case StringCompareType.Contains:
                case StringCompareType.Regex:
                    if (string.IsNullOrEmpty(baseStr))
                    {
                        //Log.Warn($"matched 判断字符串包含/开始/结束/正则时，baseStr不能为空! strToBeChecked:{strToBeChecked}");
                        //throw new Exception($"matched 判断字符串包含/开始/结束/正则时，baseStr不能为空! strToBeChecked:{strToBeChecked}");
                        return false;
                    }
                    break;
            }


            switch (compareType)
            {
                case StringCompareType.Equal:
                    matched = strToBeChecked.Equals(baseStr);
                    break;
                case StringCompareType.StartsWith:
                    matched = strToBeChecked.StartsWith(baseStr);
                    break;
                case StringCompareType.EndsWith:
                    matched = strToBeChecked.EndsWith(baseStr);
                    break;
                case StringCompareType.Contains:
                    matched = strToBeChecked.Contains(baseStr);
                    break;
                case StringCompareType.Regex:
                    Regex reg = ignoreCase ? new Regex(baseStr, RegexOptions.IgnoreCase) : new Regex(baseStr);
                    matched = reg.IsMatch(strToBeChecked);
                    break;
                case StringCompareType.Blank:
                    matched = string.IsNullOrWhiteSpace(strToBeChecked);
                    break;
                case StringCompareType.NotBlank:
                    matched = !string.IsNullOrWhiteSpace(strToBeChecked);
                    break;
                case StringCompareType.Any:
                    matched = true;
                    break;
                default:
                    //Log.Error($"ListStringComparator illegal comparator:{compareType} ListStringComparator001");
                    throw new Exception($"ListStringComparator illegal comparator:{compareType}");
            }
            return matched;
        }

        public static string GetiBotSaveValue(this StringCompareType compareType)
        {

            switch (compareType)
            {
                case StringCompareType.Equal:
                    return "eq";
                case StringCompareType.StartsWith:
                    return "startsWith";
                case StringCompareType.EndsWith:
                    return "endsWith";
                case StringCompareType.Contains:
                    return "contains";
                case StringCompareType.Regex:
                    return "regex";
                case StringCompareType.Blank:
                    return "blank";// 空白，比较时忽略源字符串。如果是null，或者只是有半角空格，也算空。
                case StringCompareType.NotBlank:
                    return "notBlank"; // 不是空白，比较时忽略源字符串。只要不是空（也不全是半角空格，也不是null），就可以匹配
                case StringCompareType.Any:
                    return "any";
                default:
                    throw new NotImplementedException($"不支持的字符串比对 {compareType}");
            }

        }

    }
}
